package com.woniu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.woniu.entity.UserInfos;
import com.woniu.service.CustomerService;
import com.woniu.service.impl.UserInfosServiceImpl;
import com.woniu.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 李桢
 * @since 2021-03-29
 */
@RestController
public class UserInfosController {

    @Autowired
    UserInfosServiceImpl userService;

    @Autowired
    CustomerService customerService;

    @PostMapping("/user/save")
    public ResultVO AddUserInfo(@RequestBody UserInfos user){
        if (user==null){
            return ResultVO.failureForNull();
        }

        boolean b = userService.AddUserInfo(user);

        return (b?ResultVO.success():ResultVO.failure());
    }

    @PostMapping("/user/delete/{userId}")
    public ResultVO DeleteUserInfo(@PathVariable("userId") Integer userId){
        if (userId==null){
            return ResultVO.failureForNull();
        }

        //验证该用户下是否有客户，有客户无法删除
        ResultVO resultVO = customerService.GetCustomerInfoByUserId(userId);
        if (resultVO.getData()==null){
            return new ResultVO(408,"请先删除该用户下的所有客户");
        }

        boolean b = userService.removeById(userId);
        return (b?ResultVO.success():ResultVO.failure());
    }

    @PostMapping("/user/update")
    public ResultVO EditUserInfo(@RequestBody UserInfos user){
        if (user==null){
            return ResultVO.failureForNull();
        }
        boolean b = userService.updateById(user);
        return (b?ResultVO.success():ResultVO.failure());
    }

    @PostMapping("user/updatePsw")
    public ResultVO EditUserPwd(@RequestBody UserInfos user){

        Integer userId = user.getUserId();
        String userPwd = user.getUserPwd();

        if (userId==null|| StringUtils.isEmpty(userPwd)){
            return ResultVO.failureForNull();
        }

        user = userService.getById(userId);
        user.setUserPwd(userPwd);

        boolean b = userService.updateById(user);
        return (b?ResultVO.success():ResultVO.failure());
    }

    @PostMapping("user/nameUseful")
    public ResultVO NameIsUseful(@RequestBody String userName){
        if (StringUtils.isEmpty(userName)){
            return ResultVO.failureForNull();
        }
        QueryWrapper<UserInfos> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_name", userName);
        UserInfos user = userService.getOne(queryWrapper);
        return (user==null?ResultVO.success(true):ResultVO.success(false));
    }

    @PostMapping("user/find")
    public ResultVO GetUserInfoByUserNameAndRole(@RequestBody UserInfos user){

        System.out.println("..................................................");
        if (user==null){
            user = new UserInfos();
        }
        //根据user对象内容，进行弹性可变的查询
        QueryWrapper<UserInfos> queryWrapper = new QueryWrapper<>(user);
        List<UserInfos> users = userService.list(queryWrapper);

        return ResultVO.success(users);
    }

    @GetMapping ("user/verify/{userName}/{userPsw}")
    public ResultVO verifyUser(@PathVariable("userName") String userName, @PathVariable("userPsw") String userPsw){
        System.out.println("userName = " + userName);
        System.out.println("userPsw = " + userPsw);
        UserInfos user = new UserInfos();
        user.setUserName(userName);
        user.setUserPwd(userPsw);
        QueryWrapper<UserInfos> queryWrapper = new QueryWrapper<>(user);
        List<UserInfos> users = userService.list(queryWrapper);
        if(users.size()==0){
            return ResultVO.failure();
        }else {
            return ResultVO.success();
        }
    }

}